﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Business.EditOrder;
using Model.Entities;
using Nethouse.Lib.Mvp;

namespace SampleWinForm
{
    public partial class FrmEditOrder 
        //: Form
        : MvpWinForm<IEditOrderView>, IEditOrderView
    {
        public event MvpEventHandler CompanyChanged;
        public event MvpEventHandler OrderChanged;

        public FrmEditOrder()
        {
            InitializeComponent();
            cobCustomer.SelectedIndexChanged += delegate { ActivateEvent(CompanyChanged); };
            cobOrder.SelectedIndexChanged += delegate { ActivateEvent(OrderChanged); };

        }

        public string OrderNumber { get { return tbOrderNo.Text; } set { tbOrderNo.Text = value; } }

        public IList<Company> CompanyList { set { SetCompanyList(value); }}
        public int CurrentCompany { get { return GetCurrentCompany(); } set { SetCurrentCompany(value); } }

        private void SetCompanyList(IList<Company> companyList)
        {
            Dictionary<int,string> localList = new Dictionary<int, string>();
            localList.Add(0, "<Välj kund>");
            foreach (var company in companyList)
            {
                localList.Add(company.Id, company.Name);
            }

            cobCustomer.DataSource = new BindingSource(localList, null);
            cobCustomer.DisplayMember = "Value";
            cobCustomer.ValueMember = "Key";
        }


        private void SetCurrentCompany(int value)
        {
            cobCustomer.SelectedValue = value;
        }

        private int GetCurrentCompany()
        {
            return (int)cobCustomer.SelectedValue;
        }

        public IList<Order> OrderList { set { SetOrderList(value); } }
        public int CurrentOrder { get { return GetCurrentOrder(); } set { SetCurrentOrder(value); } }

        private void SetOrderList(IList<Order> orderList)
        {
            Dictionary<int, string> localList = new Dictionary<int, string>();
            localList.Add(0, "<Välj order>");
            foreach (var order in orderList)
            {
                localList.Add(order.Id, order.OrderNumber);
            }

            cobOrder.DataSource = new BindingSource(localList, null);
            cobOrder.DisplayMember = "Value";
            cobOrder.ValueMember = "Key";
        }


        private void SetCurrentOrder(int value)
        {
            cobOrder.SelectedValue = value;
        }

        private int GetCurrentOrder()
        {
            return ((KeyValuePair<int, string>)cobOrder.SelectedItem).Key;
        }

        public IList<OrderRow> OrderRowList { set { SetOrderRowList(value); } }

        private void SetOrderRowList(IList<OrderRow> orderRowList)
        {
            var localList = orderRowList.Select(item => new
            {
                Namn = item.Product.Name,
                Antal = item.Count
            });
            dgvOrderItems.DataSource = new BindingSource(localList, null);
        }
    }
}
